/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Eletro Shopping
 */
public abstract class GenericDAO {
    
    PreparedStatement ps = null;
    ResultSet rs = null;
    
    private static final long serialVersionUID = 1L;
    
    public Connection getConnection(){
        try {
            Class.forName("org.postgresql.Driver");
            Connection conn = null;
            conn = DriverManager.getConnection("jdbc:postgresql://localhost/PetShop", "postgres", "321321");
            return conn;
        } catch (Exception ex) {
            Logger.getLogger(GenericDAO.class.getName()).log(Level.SEVERE,null, ex);
            return null;
        }
    }
    
    public Statement getStatement() throws SQLException{
        return getConnection().createStatement();
    }
    
    public PreparedStatement getStatement(String st) throws SQLException {
        return getConnection().prepareStatement(st);
    }
    
    public ResultSet executeQuery(String query,Object... params) throws SQLException {
         ps = getStatement(query);
        for (int i = 0; i < params.length; i++) {
            ps.setObject(i+1, params[i]);
        }
        return ps.executeQuery();
    }
    
     public int executeCommand(String query,Object... params) throws SQLException {
         ps = getStatement(query);
        for (int i = 0; i < params.length; i++) {
            try {
            ps.setObject(i+1, params[i]);
            } catch (Exception e) {
                System.out.println("Error to try "+i+" with value "+params[i]);
            }
        }
        int result = ps.executeUpdate();
        ps.close();
        return result;
    }

    public Integer getNextId(String tableName) throws SQLException {
         rs = executeQuery("select MAX(ID) from "+tableName);
        rs.next();
        Object result = rs.getObject(1);
        if (result == null) {
            rs.close();
            return 1;
        } else {
            return ((Integer)result)+1;
        }
    }

}
